//
//  BasicNetManager.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/9.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "BasicNetManager.h"
#import <AFNetworking.h>

@interface BasicNetManager ()

@property (nonatomic, strong) AFHTTPSessionManager *manager;

@end

@implementation BasicNetManager

+ (instancetype)share {
    static BasicNetManager *basic;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        basic = [[BasicNetManager alloc] init];
    });
    return basic;
}

- (instancetype)init {
    if (self = [super init]) {
        AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
        AFJSONResponseSerializer *se = [AFJSONResponseSerializer serializer];
        se.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"image/jpeg", @"image/png", @"text/html", @"text/json", @"text/javascript",@"text/plain", nil];
        manager.responseSerializer = se;
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        _manager = manager;
    }
    return self;
}

- (void)getURL:(NSString *)url param:(NSDictionary *)params completer:(void (^)(id result, NSError *error))completer {
    [self.manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (completer) {
            completer(responseObject, nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (completer) {
            completer(nil, error);
        }
    }];
}

- (void)postURL:(NSString *)url param:(NSDictionary *)params completer:(void (^)(id result, NSError *error))completer {
    [self.manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError *error;
        NSDictionary *resultDic ;
        NSLog(@"error = %@" , error);
        NSLog(@"out result = %@",responseObject);
        if( [[responseObject objectForKey:@"state"] integerValue] == 1) {
            resultDic = [responseObject objectForKey:@"data"];
            NSLog(@"result = %@",responseObject);
        }else {
            if ([responseObject objectForKey:@"msg"]) {
               error = [NSError errorWithDomain:@"服务器内部错误" code:0 userInfo:@{NSLocalizedDescriptionKey:[responseObject objectForKey:@"msg"]}];
            }
        }
        if (completer) {
            completer(resultDic, error);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       NSError  *newError;
            //NSData *data = [NSData dataWithContentsOfURL:<#(nonnull NSURL *)#>]
            if (completer) {
                NSLog(@"sevice = error");
                NSLog(@"error code = %li", error.code);
                NSLog(@"error domain = %@", error.domain);
                NSLog(@"error userinfo = %@", error.userInfo);
               if( error.code == -1001) {
                   newError = [NSError errorWithDomain:@"服务器内部错误" code:error.code userInfo:@{NSLocalizedDescriptionKey:@"请求超时！"}];
               }else {
                   newError = [NSError errorWithDomain:@"服务器内部错误" code:error.code userInfo:@{NSLocalizedDescriptionKey:@"服务器错误！"}];
               }
                completer(nil, newError);
        }
    }];
}
@end
